package cn.sccl.dirms.dev.controller;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import cn.sccl.dirms.dev.model.Chart;
import cn.sccl.dirms.dev.service.ChartManager;

/**
 * 的Controller
 * Created by liupan on 2017-06-24.
 */
@RestController
@RequestMapping(value = "/dirms/dev/chart")
public class ChartController extends BaseController {

    @Autowired
    private ChartManager chartManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Chart> listAllChart() {
        return chartManager.listAll();
    }

    /**
     * 分页查询，分页参数放置在url路径中
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<Chart> pageQueryChart(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return chartManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 分页查询，分页参数放置在request中
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public PageResult<Chart> pageQueryChart(HttpServletRequest request) {
        return chartManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));
    }

    /**
     * 保存数据
     *
     * @param chart
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Chart saveChart(@RequestBody Chart chart) {
        chartManager.save(chart);
        return chart;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @JsonView(Object.class)
    public void deleteChart(@PathVariable("id") String id) {
        chartManager.delete(id);
    }
}
